package com.bjdd.jdy.ui.sign;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.Constant;
import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;
import com.google.zxing.activity.CaptureActivity;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 客户签收
 */
public class SignActivity extends BaseActivity<SignPresenter, SignModel> implements SignContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.toolbar)
    AutoRelativeLayout toolbar;
    @Bind(R.id.et_receving_code)
    EditText etCode;
    @Bind(R.id.rl_receving_commit)
    AutoRelativeLayout rlCommit;
    @Bind(R.id.ll_receving_qrcode)
    AutoLinearLayout llQrcode;
    @Bind(R.id.tv_code_prompt)
    TextView tvPrompt;
    private String orderId;
    private CountDownTimer timer;
    @Override
    public int getLayoutId() {
        return R.layout.activity_sign;
    }
    @Override
    public void initView() {
        title.setText("客户签收");
        toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
        Bundle bundle = getIntent().getExtras();
        orderId = bundle.getString("orderId");
    }


    @OnClick({R.id.toolbar_back, R.id.rl_receving_commit, R.id.ll_receving_qrcode})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.rl_receving_commit:
                String code = etCode.getText().toString();
                if (TextUtils.isEmpty(code) || code.equals("")){
                    T.show("请先填写验证码");
                    return;
                }
                presenter.sign(orderId,code);
                break;
            case R.id.ll_receving_qrcode:
                /*Intent intent = new Intent(this, CaptureActivity.class);
                startActivityForResult(intent, 100);*/
//                JumpUtil.toSignScan(context);
                presenter.sendSMS(orderId, SpUtil.getUser().getCarrierId());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        L.d("-------------------onActivityResult----------------------requestCode : " + resultCode + "-------------requestCode : " + requestCode + "------------RESULT_OK : " + RESULT_OK);
        if (requestCode == 100 && data != null) {
            Bundle bundle = data.getExtras();
            L.d("bundle对象：" + bundle.toString());
            for (String key : bundle.keySet()) {
                L.d("Bundle Content--------------------" + "Key=" + key + ", content=" + bundle.getString(key));
            }
            String scanResult = bundle.getString("qr_scan_result");
            etCode.setText(scanResult);
            L.d("扫码返回：" + scanResult);
        }
    }

    @Override
    public void signSuccess() {
        RxManager rxManager = new RxManager();
        rxManager.post(Constant.SIGN_ORDER_BACK,"");
        finish();
    }

    @Override
    public void sendSuccess() {
        long timeAll = 60*1000;
        long timeSpace = 1000;
        llQrcode.setClickable(false);
        timer = new CountDownTimer(timeAll,timeSpace) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvPrompt.setText("("+millisUntilFinished/timeSpace+")重新获取");
            }

            @Override
            public void onFinish() {
                llQrcode.setClickable(true);
                tvPrompt.setText("获取验证码");
            }
        };
        timer.start();
    }

    @Override
    protected void onDestroy() {
        if(timer!=null){
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }
}
